using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BodySoundSimulator.CaseData;

namespace BodySoundSimulator.CommonElements
{
    public partial class LoadCaseDialog : Form
    {
        #region Fields
        Dictionary<string, CaseInformation> cases;
        bool configInfoEditable = true;
        #endregion

        #region Constructor
        public LoadCaseDialog(Dictionary<string, CaseInformation> cases,
            bool editable)
        {
            InitializeComponent();
            this.cases = cases;
            this.configInfoEditable = editable;
            UpdateUI();
        }
        #endregion

        #region Properties

        public CaseInformation SelectedCaseInfo
        {
            get 
            {
                return this.lstCases.SelectedItems[0].Tag as CaseInformation;
            }
        }

        #endregion

        #region Private methods

        private void UpdateUI()
        {
            this.lstCases.Clear();
            foreach (string key in this.cases.Keys)
            {
                ListViewItem item = new ListViewItem();
                CaseInformation foundCase = this.cases[key];
                item.Text = string.Format("{0}\n{1}\n{2}",
                    key,
                    foundCase.DiseaseName,
                    foundCase.SoundName);
                item.Tag = foundCase;
                item.ImageIndex = 0;
                this.lstCases.Items.Add(item);
            }
            if (!this.configInfoEditable)
            {
                this.btnRemoveAll.Visible = false;
                this.btnRemove.Visible = false;
            }
        }

        private void UpdateUiOnCaseSelection()
        {
            if (this.lstCases.SelectedItems != null
                && this.lstCases.SelectedItems.Count > 0)
            {
                this.btnOk.Enabled = true;
                this.btnRemove.Enabled = true;
                CaseInformation selectedCaseInfo = (CaseInformation)this.lstCases.SelectedItems[0].Tag;
                this.txtCaseDescription.Text = string.Format(SimulatorFileManager.GetInfoFormatString("CaseDisplayInfo.txt"),
                    selectedCaseInfo.CaseID,
                    selectedCaseInfo.DiseaseName,
                    selectedCaseInfo.SoundName,
                    selectedCaseInfo.PatientsClaim);
            }
            else
            {
                this.btnOk.Enabled = false;
                this.btnRemove.Enabled = false;
            }
        }

        private void RemoveCaseInformation(CaseInformation caseInfo)
        {
            SimulatorFileManager.RemoveCaseInformation(caseInfo.CaseID);
            this.cases.Remove(caseInfo.CaseID);
        }

        #endregion

        #region UI event handlers

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void lstCases_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateUiOnCaseSelection();
        }

        private void lstCases_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.lstCases.SelectedItems != null
                && this.lstCases.SelectedItems.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
           CaseInformation caseInfo = this.lstCases.SelectedItems[0].Tag as CaseInformation;
           RemoveCaseInformation(caseInfo);
           UpdateUI();
           UpdateUiOnCaseSelection();
        }

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstCases.Items)
            {
                CaseInformation caseInfo = item.Tag as CaseInformation;
                RemoveCaseInformation(caseInfo);
            }
            UpdateUI();
            UpdateUiOnCaseSelection();
        }
        #endregion
    }
}